package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    //查询用户列表
    @Override
    public PageResult<User> userList(UserDto userDto) {
        //设置参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        //调用mapper接口，执行查询
        List<User> userList = userMapper.selectList(userDto);
        //解析并分装分页结果
        Page< User> page=(Page<User>) userList;
        return new PageResult<>(page.getTotal(),page.getResult());
    }
    //用户批量删除
    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }
    //添加用户
    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password =user.getUsername()+"123"; // 设置默认密码
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
              userMapper.insert(user);
    }
    //根据ID查询用户
    @Override
    public User getByID(Integer id) {
        return  userMapper.selectByID(id);
    }
    //修改用户
    @Override
    public void upDate(User user) {
        user.setUpdateTime(LocalDateTime.now());
        String password =user.getUsername()+"123"; // 设置默认密码
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.upDate(user);
    }
    //查询所有用户
    @Override
    public Result allList(User user) {
     return Result.success(userMapper.selectAllList());
    }
    //根据角色查询用户
    @Override
    public Result roleList(String roleLabel) {
        return Result.success(userMapper.roleList(roleLabel));
    }
    //根据部门查询用户
    @Override
    public Result deptList(Integer deptId) {
        return Result.success(userMapper.deptList(deptId));
    }
    //登录
    @Override
    public LoginResultVo login(User user) {
        //1.调用Mapper接口查询数据库
        User user1 = userMapper.selectByUsername(user.getUsername());
        //2.判断用户名
        if(!user.getUsername().equals(user1.getUsername())){
            log.info("用户名或密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        //3.判断密码
        String s = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!s.equals(user1.getPassword())){
            log.info("用户名或密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        //4.判断状态是否停用
        if(user1.getStatus()==0){
            log.info("用户已停用");
           throw new BusinessException("用户已停用");
        }
        //5.封装对象返回结果
        LoginResultVo loginResultVo=new LoginResultVo();
        loginResultVo.setId(user1.getId());
        loginResultVo.setUsername(user1.getUsername());
        loginResultVo.setName(user1.getName());
        loginResultVo.setImage(user1.getImage());
        loginResultVo.setRoleLabel(user1.getRoleLabel());

        //6.生成token
        Map<String,Object>claims=new HashMap<>();
        claims.put("id",user1.getId());
        claims.put("username",user1.getUsername());
        claims.put("name",user1.getName());
        String jwt = JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}